package com.bodacn.keypoint.adapter;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.bodacn.keypoint.R;
import com.bodacn.keypoint.bean.ClothPieceBean;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class ClothPieceLvAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<ClothPieceBean> list;
    private DecimalFormat fnum;

    public ClothPieceLvAdapter(List<ClothPieceBean> list, Context context) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.list = list;
        fnum = new DecimalFormat("##0.0");
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHodler;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_cloth_piece_top, parent, false);
            viewHodler = new ViewHolder(convertView);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHolder) convertView.getTag();
        }

        String fillScreen = list.get(position).getFillScreen();
        final String beforeSewH = list.get(position).getBeforeSewH();
        final String NormalSizeH = list.get(position).getNormalSizeH();
        String WeightSizeH = list.get(position).getWeightSizeH();
        final String ScaleAddH = list.get(position).getScaleAddH();
        String PartName = list.get(position).getPartName();
        final String ScaleAddV = list.get(position).getScaleAddV();
        String WeightSizeV = list.get(position).getWeightSizeV();
        final String NormalSizeV = list.get(position).getNormalSizeV();
        final String BeforeSewV = list.get(position).getBeforeSewV();
        String Ratio = list.get(position).getRadio();

        viewHodler.tvPartName.setText(PartName);

        viewHodler.tvNormalV.setText(NormalSizeV + "");
        viewHodler.tvNormalH.setText(NormalSizeH + "");

        viewHodler.tvScaleH.setText(ScaleAddH + "");
        viewHodler.tvScaleV.setText(ScaleAddV + "");

        viewHodler.tvWeihtV.setText(WeightSizeV + "");
        viewHodler.edWeightH.setText(WeightSizeH + "");

        viewHodler.edBeforeH.setText(beforeSewH + "");
        viewHodler.edBrforeV.setText(BeforeSewV + "");
        Log.e("11111", "BeforeSewH" + beforeSewH + ",Ratio" + Ratio + "," +
                "NormalSizeh" + NormalSizeH + ",NormalSizeV1" + NormalSizeV + ",BeforeSewV" + BeforeSewV);
        if (list.get(position).getCheckResult() == "1") {
            viewHodler.tvCheckresult.setBackgroundColor(mContext.getResources().getColor(R.color.green));
            viewHodler.tvCheckresult.setText("合格");
        }
        if (list.get(position).getCheckResult() == "1") {
            viewHodler.tvCheckresult.setBackgroundColor(mContext.getResources().getColor(R.color.red));
            viewHodler.tvCheckresult.setText("不合格");
        }
        //判断直是不是合格
        if (!TextUtils.isEmpty(WeightSizeV) && !TextUtils.isEmpty(Ratio) && !TextUtils.isEmpty(NormalSizeV) &&
                !TextUtils.isEmpty(WeightSizeH) && !TextUtils.isEmpty(NormalSizeH)) {
            if (Float.parseFloat(BeforeSewV) >= Float.parseFloat(NormalSizeV) - Float.parseFloat(Ratio)
                    && Float.parseFloat(BeforeSewV) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeV)
                    && Float.parseFloat(beforeSewH) >= Float.parseFloat(NormalSizeH) - Float.parseFloat(Ratio)
                    && Float.parseFloat(beforeSewH) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeH)) {
                list.get(position).setCheckResult("1");
                viewHodler.tvCheckresult.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                viewHodler.tvCheckresult.setText("合格");
            } else {
                list.get(position).setCheckResult("0");
                viewHodler.tvCheckresult.setBackgroundColor(mContext.getResources().getColor(R.color.red));
                viewHodler.tvCheckresult.setText("不合格");
            }
        }

        viewHodler.ivHAdd.setTag(position);
        viewHodler.ivHAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResultH(beforeSewH, NormalSizeH, viewHodler.ivHAdd, viewHodler.tvCheckresult, "add");
            }
        });
        viewHodler.ivHPlus.setTag(position);
        viewHodler.ivHPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResultH(beforeSewH, NormalSizeH, viewHodler.ivHAdd, viewHodler.tvCheckresult, "plus");
            }
        });

        viewHodler.ivVAdd.setTag(position);
        viewHodler.ivVAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResultV(BeforeSewV, NormalSizeV, viewHodler.ivVAdd, viewHodler.tvCheckresult, "add");
            }
        });

        viewHodler.ivVPlus.setTag(position);
        viewHodler.ivVPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckResultV(BeforeSewV, NormalSizeV, viewHodler.ivVAdd, viewHodler.tvCheckresult, "plus");
            }
        });

        return convertView;
    }

    private void CheckResultV(String beforeSewV, String NormalSizeV, ImageView iv, TextView tvResult, String type) {
        if (!TextUtils.isEmpty(beforeSewV + "") && !TextUtils.isEmpty(NormalSizeV + "")
              ) {
            Float bh = Float.parseFloat(beforeSewV);
            if (bh > 0.0f) {
                if (type.equals("add")) {
                    bh = bh + 0.1f;
                } else {
                    bh = bh - 0.1f;
                }
                String last = fnum.format(bh);
                int currentP = (int) iv.getTag();
                list.get(currentP).setBeforeSewV(last);
                String weightLast = fnum.format(
                         Float.parseFloat(last)-Float.parseFloat(NormalSizeV));
                list.get(currentP).setWeightSizeV(weightLast);

                //直点击加减结束后判断是合格还是不合格
                if (!TextUtils.isEmpty(list.get(currentP).getBeforeSewH())
                        && !TextUtils.isEmpty(list.get(currentP).getRadio())
                        && !TextUtils.isEmpty(list.get(currentP).getNormalSizeH())
                        && !TextUtils.isEmpty(list.get(currentP).getBeforeSewV())
                        && !TextUtils.isEmpty(list.get(currentP).getNormalSizeV())) {
                    String BeforeSewH = list.get(currentP).getBeforeSewH();
                    String Ratio = list.get(currentP).getRadio();
                    String NormalSizeh = list.get(currentP).getNormalSizeH();
                    String NormalSizeV1 = list.get(currentP).getNormalSizeV();
                    String BeforeSewV = list.get(currentP).getBeforeSewV();
                    Log.e("11111", "BeforeSewH" + BeforeSewH + ",Ratio" + Ratio + "," +
                            "NormalSizeh" + NormalSizeh + ",NormalSizeV1" + NormalSizeV1 + ",BeforeSewV" + BeforeSewV);
                    if (Float.parseFloat(BeforeSewH) >= Float.parseFloat(NormalSizeh) - Float.parseFloat(Ratio)
                            && Float.parseFloat(BeforeSewH) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeh)
                            && Float.parseFloat(BeforeSewV) >= Float.parseFloat(NormalSizeV1) - Float.parseFloat(Ratio)
                            && Float.parseFloat(BeforeSewV) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeV1)) {
                        list.get(currentP).setCheckResult("1");
                        tvResult.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                        tvResult.setText("合格");
                        Log.e("11111", "合格");
                    } else {
                        Log.e("11111", "不合格");
                        list.get(currentP).setCheckResult("0");
                        tvResult.setBackgroundColor(mContext.getResources().getColor(R.color.red));
                        tvResult.setText("不合格");
                    }
                }

                notifyDataSetChanged();
            }
        }
    }

    private void CheckResultH(String beforeSewH, String NormalSizeH, ImageView iv, TextView tvResult, String type) {
        if (!TextUtils.isEmpty(beforeSewH + "") && !TextUtils.isEmpty(NormalSizeH + "")
        ) {
            Float bh = Float.parseFloat(beforeSewH);
            if (bh > 0.0f) {
                if (type.equals("add")) {
                    bh = bh + 0.1f;
                } else {
                    bh = bh - 0.1f;
                }
                String last = fnum.format(bh);
                int currentP = (int) iv.getTag();
                list.get(currentP).setBeforeSewH(last);

                String weightLast = fnum.format(Float.parseFloat(last) - Float.parseFloat(NormalSizeH)
                );
                list.get(currentP).setWeightSizeH(weightLast);
                //横 点击加减结束后判断是合格还是不合格
                if (!TextUtils.isEmpty(list.get(currentP).getBeforeSewH())
                        && !TextUtils.isEmpty(list.get(currentP).getRadio())
                        && !TextUtils.isEmpty(list.get(currentP).getNormalSizeH())
                        && !TextUtils.isEmpty(list.get(currentP).getBeforeSewV())
                        && !TextUtils.isEmpty(list.get(currentP).getNormalSizeV())) {
                    String BeforeSewH = list.get(currentP).getBeforeSewH();
                    String Ratio = list.get(currentP).getRadio();
                    String NormalSizeh = list.get(currentP).getNormalSizeH();
                    String NormalSizeV1 = list.get(currentP).getNormalSizeV();
                    String BeforeSewV = list.get(currentP).getBeforeSewV();
                    Log.e("111122", "BeforeSewH" + BeforeSewH + ",Ratio" + Ratio + "," +
                            "NormalSizeh" + NormalSizeh + ",NormalSizeV1" + NormalSizeV1 + ",BeforeSewV" + BeforeSewV);
                    if (Float.parseFloat(BeforeSewH) >= Float.parseFloat(NormalSizeh) - Float.parseFloat(Ratio)
                            && Float.parseFloat(BeforeSewH) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeh)
                            && Float.parseFloat(BeforeSewV) >= Float.parseFloat(NormalSizeV1) - Float.parseFloat(Ratio)
                            && Float.parseFloat(BeforeSewV) <= Float.parseFloat(Ratio) + Float.parseFloat(NormalSizeV1)) {
                        list.get(currentP).setCheckResult("1");
                        tvResult.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                        tvResult.setText("合格");
                        Log.e("1111122", "合格");
                    } else {
                        list.get(currentP).setCheckResult("0");
                        tvResult.setBackgroundColor(mContext.getResources().getColor(R.color.red));
                        tvResult.setText("不合格");
                        Log.e("1111122", "不合格");
                    }
                }


                notifyDataSetChanged();
            }
        }
    }


    static
    class ViewHolder {
        @BindView(R.id.tv_PartName)
        TextView tvPartName;
        @BindView(R.id.tv_normal_v)
        TextView tvNormalV;
        @BindView(R.id.tv_normal_h)
        TextView tvNormalH;
        @BindView(R.id.tv_scale_v)
        TextView tvScaleV;
        @BindView(R.id.tv_scale_h)
        TextView tvScaleH;
        @BindView(R.id.ed_brfore_v)
        TextView edBrforeV;
        @BindView(R.id.iv_v_add)
        ImageView ivVAdd;
        @BindView(R.id.iv_v_plus)
        ImageView ivVPlus;
        @BindView(R.id.ed_before_h)
        TextView edBeforeH;
        @BindView(R.id.iv_h_add)
        ImageView ivHAdd;
        @BindView(R.id.iv_h_plus)
        ImageView ivHPlus;
        @BindView(R.id.tv_weiht_v)
        TextView tvWeihtV;
        @BindView(R.id.ed_weight_h)
        TextView edWeightH;
        @BindView(R.id.tv_checkresult)
        TextView tvCheckresult;
        @BindView(R.id.mylistitem)
        LinearLayout mylistitem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
